home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Initialising structure members - help please!
- Date: 21 Feb 1996 09:42:43 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gflijINN8hi@keats.ugrad.cs.ubc.ca>
- References: <4gb8hn$3m8@news.mistral.co.uk>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4gb8hn$3m8@news.mistral.co.uk>,
- Mike Barnard <mikebarnard@mistral.co.uk> wrote:
- >Hi all.
- >
- >I'm a learner. I am trying to create a function to display a list of
- >menu items using a doubly linked list. My problem is in initialising
- >the members of the individual menu items.
- >
- >Within a function called VPCMENU.C I have created a structure...
- >
- >// Define a structure to hold the menu item information.
-
- Standard C has no operator '//'. That is a syntax error. I think you want the
- "comp.lang.c++" newsgroup down the hall to the left!
-
- >struct menuitem
- > {
- > int number; // Item 1, item 2 etc.
- > char description[70]; // Room for a text description
- > struct menuitem *next; // Pointer to next menu item
- > struct menuitem *last; // Pointer to last menu item
- > };
- >
- >Now I create 5 instances of variables of the type menuitem...
- >
- >// Define the instances of the structure.
- >
- >struct menuitem one,two,three,four,five;
- >
- >Now, the fun (not) bit. I tried to initialise the items with...
- >
- >one.number = 1;
- >one.description = "1. Calculate a minefield";
-
- That would work if description was a "char *", or if you performed the
- assignment in an declaration with an initializer rather than in a program
- statement:
-
- struct foo {
- char data[20];
- int num;
- } my_struct = { "Bar", 1 };
-
- This is only allowed (by some old compilers) if this is outside of a function,
- or preceded by a 'static' keyword. Otherwise you attempt something called
- "initialization of automatic aggregates", which is a no-no with these venerable
- old fogies.. However, ANSI allows such initialization ``...only by constant
- constructions unless the initializer can be expressed by a simple expression''.
- (K&R2 A8.7).
-
- >one.next = two;
- >one.last = five;
- >
- >The first one passes OK, but the second one creates an error. As it
- >stands it says "Lvalue required". Help says "The left side of an
- >assignment operator must be an addressable expression." I don't know
- >about the third and fourth ones. Yet.
-
- Your compiler's message is not strictly correct. The left side of the
- expression _is_ an lvalue, because it refers to an object, and it is
- addressable. But it is not a _modifiable_ lvalue. Like an array, a function is
- also an lvalue, but it isn't modifiable:
-
- [Assignment Expressions] require an lvalue as a left operand, and the
- lvalue must be modifiable: it must not be an array, and must not have
- an incomplete type, or be a function. (K&R2 A7.17)
-
- >This is an almost exact copy of the structure examples in my book.
- >(The beginners guide to C by Wrox). Page 387 for those who have it.
-
- How exact is "almost" exact? Does the Wrox book also try to do an assignment to
- an array variable?
-
- Perhaps you might avail yourself of a copy of the Kernighan and Ritchie text.
- It is very useful companion. Also, go to the FTP site rtfm.mit.edu (or a
- suitable mirror), and look for the comp.lang.c FAQ in /pub/usenet
-
- >So, can anyone help please? (I must get snippets!). This is for a PC
- >in text mode using Borland C++ 3.0. The completed program is to do
-
- Again, this is the comp.lang.c newsgroup, not C++. A strictly standard
- compiler's pre-processor won't even accept your style of writing comments.
- --
-
-